Ext.define('BEST.controller.CmsuserController', {
	extend:'Ext.app.Controller',
	views:['cmsuser.List','cmsuser.Edit'],
	stores:['CmsuserStore'],
	models:['CmsuserModel'],
	refs:[{
		ref : 'addBtn',
		selector : 'cmsuserlist button#add'
	}, {
		ref : 'editBtn',
		selector : 'cmsuserlist button#edit'
	}, {
		ref : 'delBtn',
		selector : 'cmsuserlist button#delete'
	}, {
		ref : 'stateBtn',
		selector : 'cmsuserlist button#state'
	}, {
		ref : 'saveBtn',
		selector : 'cmsuseredit button#save'
	}, {
		ref : 'cancelBtn',
		selector : 'cmsuseredit button#cancle'
	}],
	init:function(){
		this.control({
			'cmsuserlist' : {
				select : this.onSelect,
				deselect : this.onDeselect
			},
			'cmsuserlist button#edit' : {
				click : this.onEdit
			},
			'cmsuserlist button#delete' : {
				click : this.onDelete
			},
	        'cmsuserlist button#add':{
	        	click:this.onAdd
	        },
	        'cmsuseredit button#save' : {
				click : this.onSave
			},
			'cmsuseredit button#cancle' : {
				click : this.onClose
			},
			'cmsuserlist button#state' : {
				click : this.onState
			}
		});
	},
	onSelect :function(){
		this.changeBtnState(false);
	},
	onDeselect : function() {
		this.changeBtnState(true);
	},
	onAdd:function(btn){
		Ext.widget('cmsuseredit',{animateTarget:document.body,title:btn.getText()}).show();
	},
	onEdit : function(btn) {
		var win = Ext.widget('cmsuseredit', {
			animateTarget : document.body,
			title : btn.getText()
		});
		var record = btn.up('cmsuserlist').getSelectionModel()
				.getLastSelected();
		win.down('form').getForm().loadRecord(record);
		win.show();
	},
	onDelete : function(btn) {
		Ext.MessageBox.confirm('确认信息', '确认删除该记录吗?', function(
				_btn) {
			if (_btn == 'yes') {
				var record = btn.up('cmsuserlist')
						.getSelectionModel().getLastSelected();
				btn.up('cmsuserlist').getStore().remove(record);

			}
		});
	},
	onSave : function(btn) {
		var me = this;
		var win = btn.up('window');
		var form = win.down('form').getForm();
		if (form.isValid()) {
			var record = form.getRecord();
			if (record) {
				var values = form.getValues();
				record.beginEdit();
				record.set(form.getValues());
				record.endEdit();
			} else {
				record = Ext.create(me
						.getModel("BEST.model.CmsuserModel"),
						form.getValues());
				
				record.phantom = true;
				record.setDirty();
				this.getStore("BEST.store.CmsuserStore").insert(
						0, record);
			}
			win.close();
		}
	},
	onState : function(btn) {
		var me = this;
		Ext.MessageBox.confirm('确认信息', '确认更改用户状态吗?', function(
				_btn) {
			if (_btn == 'yes') {
				var record = btn.up('cmsuserlist')
						.getSelectionModel().getLastSelected();
				var curStatus=record.get('tcuStatus');
				var curTcuId=record.get('tcuId');
				var targetStatus=0;
				if(curStatus==0){
					targetStatus=1;
				}
				Ext.Ajax.request({
					method:'POST',//提交的方式,如Get，post等
					url:mainServer+'/cmsuser/updatestatus',//请求地址
					params:{ 'tcuId':curTcuId,'tcuStatus':targetStatus},
		            success:function(reps,option){
		             var repsObj=Ext.JSON.decode(reps.responseText);//此为后台成功返回的json等字符
		             if(repsObj['success']){
		            	 Ext.getStore('CmsuserStore').sync();
							Ext.getStore('CmsuserStore').reload();
							Ext.example.msg('温馨提示', '状态修改成功');
		             }
		            },//成功的回调函数    
		            failure:function(res){
		      
		            }//失败的情况
				});
			}
		});
	},
	changeBtnState : function(state) {
		this.getEditBtn().setDisabled(state);
		this.getDelBtn().setDisabled(state);
		this.getStateBtn().setDisabled(state);
	},
	onClose : function(btn) {
		btn.up('window').close();
	},
	showWin:function(winName, btn){
		var win = Ext.widget(winName, {
			animateTarget : document.body,
			title : btn.getText()
		});
		var record = btn.up('cmsuserlist').getSelectionModel()
				.getLastSelected();
		win.down('form').getForm().loadRecord(record);
		win.show();
	}
});